﻿using HandyControl.Tools.Extension;
using Minotaur.App.Core.Contrls;
using Minotaur.App.Modules.SystemSet.ViewModels;
using ServiceStack;
using System.Linq;
using System.Windows.Controls;

namespace Minotaur.App.Modules.SystemSet.Views
{
    /// <summary>
    /// Interaction logic for Resources
    /// </summary>
    public partial class Resources : UserControl
    {
        public Resources()
        {
            InitializeComponent();
        }
        private CheckBox AllCheckBox;
        private void ResType_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ResourcesViewModel viewModel = this.DataContext as ResourcesViewModel;
            viewModel.FormDependencyObject = this.FormDockPanel;
            viewModel.Dispatcher = this.Dispatcher;
            viewModel.ListPagination = this.pagination;
            viewModel.SideMenu = menuInfo;
            viewModel.InitData();
            AllCheckBox = CoordinateUilts.GetChildObjects<CheckBox>(this.ResorcesTreeGrid).FirstOrDefault(o => o.Name == "AllCheck");
            if (AllCheckBox != null)
            {
                AllCheckBox.Click += AllCheck_Click;
                viewModel.AllCheckBox = AllCheckBox;
            } 
        } 

        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AllCheck_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ResourcesViewModel viewModel = this.DataContext as ResourcesViewModel;
            CheckBox headercb = (CheckBox)sender;
            foreach (var o in viewModel.ResourceViews)
            {
                o.IsChecked = headercb.IsChecked.HasValue ? headercb.IsChecked.Value : false;
            }
        }

        /// <summary>
        /// 单选操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SingeCheck_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CheckBox headercb = (CheckBox)sender;
            if (headercb.IsChecked.HasValue && headercb.IsChecked.Value)
            {
                ResourcesViewModel viewModel = this.DataContext as ResourcesViewModel;
                if (!viewModel.ResourceViews.Any(o => o.IsChecked == false))
                {
                    if (AllCheckBox != null)
                    {
                        AllCheckBox.IsChecked = true;
                    }
                }
                else
                {
                    if (AllCheckBox != null)
                    {
                        AllCheckBox.IsChecked = false;
                    }
                }
            }
            else
            {
                if (AllCheckBox != null)
                {
                    AllCheckBox.IsChecked = false;
                }
            }
        }
    }
}
